<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="gUsername">
      <el-input v-model="dataForm.gUsername" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="员工姓名" prop="gEmpname">
      <el-input v-model="dataForm.gEmpname" placeholder="员工姓名"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="gDeptname">
      <el-input v-model="dataForm.gDeptname" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="模块名称" prop="gPartname">
      <el-input v-model="dataForm.gPartname" placeholder="模块名称"></el-input>
    </el-form-item>
    <el-form-item label="登录ip" prop="gLogip">
      <el-input v-model="dataForm.gLogip" placeholder="登录ip"></el-input>
    </el-form-item>
    <el-form-item label="登录时间" prop="gLogingtime">
      <el-input v-model="dataForm.gLogingtime" placeholder="登录时间"></el-input>
    </el-form-item>
    <el-form-item label="功能" prop="gFunction">
      <el-input v-model="dataForm.gFunction" placeholder="功能"></el-input>
    </el-form-item>
    <el-form-item label="操作时间" prop="gOpretime">
      <el-input v-model="dataForm.gOpretime" placeholder="操作时间"></el-input>
    </el-form-item>
    <el-form-item label="操作状态" prop="gOprestatus">
      <el-input v-model="dataForm.gOprestatus" placeholder="操作状态"></el-input>
    </el-form-item>
    <el-form-item label="终端名称" prop="gFinalname">
      <el-input v-model="dataForm.gFinalname" placeholder="终端名称"></el-input>
    </el-form-item>
    <el-form-item label="退出时间" prop="gExittime">
      <el-input v-model="dataForm.gExittime" placeholder="退出时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gId: 0,
          gUsername: '',
          gEmpname: '',
          gDeptname: '',
          gPartname: '',
          gLogip: '',
          gLogingtime: '',
          gFunction: '',
          gOpretime: '',
          gOprestatus: '',
          gFinalname: '',
          gExittime: ''
        },
        dataRule: {
          gUsername: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          gEmpname: [
            { required: true, message: '员工姓名不能为空', trigger: 'blur' }
          ],
          gDeptname: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          gPartname: [
            { required: true, message: '模块名称不能为空', trigger: 'blur' }
          ],
          gLogip: [
            { required: true, message: '登录ip不能为空', trigger: 'blur' }
          ],
          gLogingtime: [
            { required: true, message: '登录时间不能为空', trigger: 'blur' }
          ],
          gFunction: [
            { required: true, message: '功能不能为空', trigger: 'blur' }
          ],
          gOpretime: [
            { required: true, message: '操作时间不能为空', trigger: 'blur' }
          ],
          gOprestatus: [
            { required: true, message: '操作状态不能为空', trigger: 'blur' }
          ],
          gFinalname: [
            { required: true, message: '终端名称不能为空', trigger: 'blur' }
          ],
          gExittime: [
            { required: true, message: '退出时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gId) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/loginformation/info/${this.dataForm.gId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gUsername = data.loginformation.gUsername
                this.dataForm.gEmpname = data.loginformation.gEmpname
                this.dataForm.gDeptname = data.loginformation.gDeptname
                this.dataForm.gPartname = data.loginformation.gPartname
                this.dataForm.gLogip = data.loginformation.gLogip
                this.dataForm.gLogingtime = data.loginformation.gLogingtime
                this.dataForm.gFunction = data.loginformation.gFunction
                this.dataForm.gOpretime = data.loginformation.gOpretime
                this.dataForm.gOprestatus = data.loginformation.gOprestatus
                this.dataForm.gFinalname = data.loginformation.gFinalname
                this.dataForm.gExittime = data.loginformation.gExittime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/loginformation/${!this.dataForm.gId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gId': this.dataForm.gId || undefined,
                'gUsername': this.dataForm.gUsername,
                'gEmpname': this.dataForm.gEmpname,
                'gDeptname': this.dataForm.gDeptname,
                'gPartname': this.dataForm.gPartname,
                'gLogip': this.dataForm.gLogip,
                'gLogingtime': this.dataForm.gLogingtime,
                'gFunction': this.dataForm.gFunction,
                'gOpretime': this.dataForm.gOpretime,
                'gOprestatus': this.dataForm.gOprestatus,
                'gFinalname': this.dataForm.gFinalname,
                'gExittime': this.dataForm.gExittime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
